package com.spark.WorCount.sql

import org.apache.spark.SparkConf
import org.apache.spark.sql.SparkSession

object DataFrameSQLByScala {
  def main(args: Array[String]): Unit = {
      val conf = new SparkConf()
        .setMaster("local")
      //创建SparkSession对象，里面包含SparkContext和SqlContext
      val sparkSession = SparkSession.builder()
        .appName("DataFrameSQLByScala")
        .config(conf)
        .getOrCreate()
      //读取json文件，获取DataFrame
      val stuDf = sparkSession.read.json("datas/student.json")
      //将DataFrame注册成一个临时表
      stuDf.createTempView("student")
      //执行指定的SQL语句并把结果数据展示出来
      val sql = "select * from student"
      sparkSession.sql(sql).show(2)
    //关闭SparkSession链接
      sparkSession.stop()
  }
}
